package message

import (
	"context"
	"mime/multipart"
	"wave-service/server/app/common/response"
	"wave-service/server/app/common/upload"
	"wave-service/server/app/internal/svc"
	"wave-service/server/app/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type MessageUploadLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewMessageUploadLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MessageUploadLogic {
	return &MessageUploadLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *MessageUploadLogic) MessageUpload(req *types.UploadRequest, header *multipart.FileHeader) (resp *types.UploadResponse, err error) {
	oss := upload.NewOss(l.svcCtx.Config.OssType)
	path, uploadErr := oss.UploadFile(header, l.svcCtx.Config)
	if uploadErr != nil {
		logx.Errorf("上传文件错误%v", err.Error())
		return nil, response.Error(500, uploadErr.Error())
	}

	return &types.UploadResponse{Path: path}, nil
}
